﻿$(function () {
    GetMer();//加载操作员管理的所有商户
    $('#Search').linkbutton('disable');    $('#WholeCommodity').linkbutton('disable');
    $('#SearchDishesTypeName').textbox('disable');
    $('#DishesNmae').textbox('disable');
})

var DishesID;
var Mer;
//获取商户
function GetMer() {
    var grid = $('#SearchMer').combogrid({
        panelWidth: 500,
        idField: 'MerID',
        textField: 'MerName',
        url: '/Sales/getMer',
        method: 'post',
        pageSize: 100,
        pageList: [100],
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        columns: [[
            { field: 'MerID', title: '商户ID', width: 80, align: 'center' },
            { field: 'MerName', title: '商户名称', width: 80, align: 'center' },
            { field: 'MerTxt', title: '商户介绍', width: 120, align: 'center' },
            { field: 'MerAdr', title: '商户地址', width: 80, align: 'center' },
            { field: 'MerCall', title: '商户联系电话', width: 80, align: 'center' }
        ]],
        onSelect: function () {
            Mer = $('#SearchMer').combogrid("getValue");
            DishesTabledetail(Mer);
            $('#Search').linkbutton('enable');            $('#WholeCommodity').linkbutton('enable');
            $('#SearchDishesTypeName').textbox('enable');
            $('#DishesNmae').textbox('enable');
        } 
    });
    //    onSelect: function (index, row) {
    //        diplayText = row[grid.combogrid('options').textField];
    //        Mer = $('#SearchMer').combogrid("getValue");//获取操作员
    //        //加载对应的商品
    //        initTable(Mer);
    //        //加载对应的商品分类
    //        GetOrderType(Mer);
    //        //控制增删查改的权限
    //        $.post("/Order_Shelves/getPower", function (database) {
    //            if (database != "") {
    //                $('#btndetail').linkbutton('enable');
    //                if (database.Add == 1) {
    //                    $('#btnadd').linkbutton('enable');
    //                }
    //                if (database.Dell == 1) {
    //                    $('#btndell').linkbutton('enable');
    //                }
    //                if (database.Edit == 1) {
    //                    $('#btnedit').linkbutton('enable');
    //                }
    //            }
    //        })
    //    },
    //    onLoadSuccess: function () {
    //        //分页后判断下是否有选中的数据行，combogrid会依据上一次选中的值进行初始化，选中datagrid数据行和设置显示值
    //        if (!grid.combogrid('grid').datagrid('getSelected'))//不在当前页
    //        {
    //            $('#SearchMer').combogrid("setValue", diplayText);
    //        }
    //    }
        //});

}

// 查看全部
function WholeCommodity() {
    $('#SearchDishesTypeName').textbox('setValue', "");
    $('#DishesNmae').textbox('setValue', "");
    DishesTabledetail(Mer);
}

//商品上架数量datagrid 
function DishesTabledetail(itme) {
    DishesTypeNameCombobox();
    $('#dd').datagrid({
        loadMsg: '数据加载中...',
        fitColumns: false,
        nowrap: true,
        autoRowHeight: true,
        striped: true,
        url: '/Sales/GetShelves?MerID=' + itme,
        sortName: 'DishesID',
        sortOrder: 'asc',
        singleSelect: true,
        border: true,
        remoteSort: false,
        idField: 'DishesID',
        pageSize: 100,
        pagination: true,
        pageList: [100],
        rownumbers: true,
        toolbar: '#tb',
        onBeforeEdit: function (index, row) {
            row.editing = true;
            updateActions(index);
        },
        onAfterEdit: function (index, row) {
            row.editing = false;
            updateActions(index);
        },
        onCancelEdit: function (index, row) {
            row.editing = false;
            updateActions(index);
        },
        columns: [[
            { field: 'DishesID', title: '商品ID', width: 80, align: 'center' },
            { field: 'DishesTypeName', title: '商品分类', width: 80, align: 'center' },
            { field: 'DishesNmae', title: '商品名称', width: 120, align: 'center' },
            { field: 'SaleNub', title: '上架数量', width: 80, align: 'center' },
            {
                field: 'Sold', title: '已售数量', width: 100, sortable: true, align: 'center', formatter: function (value, row) {
                    var sold = Math.abs(value);
                    return Math.abs(value);
                }
            },
            { field: 'HaveNub', title: '剩余数量', width: 80, align: 'center', editor: { type: 'numberspinner' } },
            {
                field: 'OrgID', title: '修改剩余数量', width: 100, sortable: true, align: 'center', formatter: function (value, row, index) {
                    DishesID = row.DishesID;
                    if (row.editing) {
                        var s = '<a href="#" onclick="saverow(this)">保存</a> ';
                        var c = '<a href="#" onclick="cancelrow(this)">取消</a>';
                        return s + c;
                    } else {
                        var e = '<a href="#" onclick="editrow(this)">编辑</a> ';
                        return e;
                    }
                }
            },
            {
                field: 'OrgID2', title: '售罄', width: 100, sortable: true, align: 'center', formatter: function (value, row) {
                    return '<button onclick="SoldOut(' + row.DishesID + ',\'' + row.DishesNmae + '\')" style="padding :0 16px">售罄</button>';
                }
            },
        ]]
    });
}

//更新
function updateActions(index) {
    $('#dd').datagrid('updateRow', {
        index: index,
        row: {}
    });
}

//获取当前编辑的行
function getRowIndex(target) {
    var tr = $(target).closest('tr.datagrid-row');
    return parseInt(tr.attr('datagrid-row-index'));
}

//编辑行
function editrow(target) {
    $('#dd').datagrid('beginEdit', getRowIndex(target));
}

//保存行编辑信息
function saverow(target) {
    $('#dd').datagrid('endEdit', getRowIndex(target));
}

//取消行编辑
function cancelrow(target) {
    $('#dd').datagrid('cancelEdit', getRowIndex(target));
}

//进行编辑datagrid时设置
$.extend($.fn.datagrid.defaults.editors, {
    numberspinner: {
        init: function (container, options) {
            var input = $('<input type="text">').appendTo(container);
            return input.numberspinner(options);
        },
        destroy: function (target) {
            $(target).numberspinner('destroy');
        },
        getValue: function (target) {
            Update($(target).numberspinner('getValue'));
            return $(target).numberspinner('getValue');
        },
        setValue: function (target, value) {
            $(target).numberspinner('setValue', value);
        },
        resize: function (target, width) {
            $(target).numberspinner('resize', width);
        }
    }
});

// 修改剩余数量
function Update(HasNub) {
    $.post("/Sales/SoldOut", { DishesID: DishesID, HasNub: HasNub }).success(function (resp) {
        if (resp == "1") {
            $.messager.alert("友情提示", "修改成功！");
            DishesTabledetail(Mer);
        }
        else {
            $.messager.alert("友情提示", "修改失败！");
        }
    });
}

// 售罄
function SoldOut(DishesID, DishesNmae) {
    $.messager.confirm("友情提示", "您确认售罄<font color='red' size='6'>" + DishesNmae + "</font>这个商品", function (Del) {
        if (Del) {
            $.post("/Sales/SoldOut", { DishesID: DishesID, HasNub: 0 }).success(function (resp) {
                if (resp == "1") {
                    $.messager.alert("友情提示", "修改成功！");
                    DishesTabledetail(Mer);
                }
                else {
                    $.messager.alert("友情提示", "修改失败！");
                }
            });
        }
    });
}

// 商品分类下拉框
function DishesTypeNameCombobox() {
    $.post("/Sales/getOrderType", { MerID: Mer }, function (database) {
        var a = {
            "DishesTypeID": "-8989",
            "DishesTypeName": "全部",
        }

        //database.push(a)
        database.splice(0, 0, a); // 将全部的选项放在最前
        $('#SearchDishesTypeName').combobox({
            valueField: 'DishesTypeID',
            textField: 'DishesTypeName',
            value: '-8989',
            text: '全部',
            data: database,
            multiple: false
        });
    })
}

// 查询
function Search() {
    var DishesTypeID = $("#SearchDishesTypeName").textbox('getValue');
    var DishesNmae = $("#DishesNmae").val();
    $('#dd').datagrid({
        url: '/Sales/Search?DishesTypeID=' + DishesTypeID + "&DishesNmae=" + DishesNmae + "&MerID=" + Mer,
    });
}
